package com.atguigu.educms.controller;


import com.atguigu.commonutils.R;
import com.atguigu.educms.entity.CmsBanner;
import com.atguigu.educms.service.CmsBannerService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-06-23
 */
@RestController
@RequestMapping("/educms/banneradmin")
//@CrossOrigin
public class CmsBannerAdminController {

    @Autowired
    CmsBannerService cmsBannerService;

    //获取分页数据
    @GetMapping("getListByPage/{page}/{size}")
    public R getListByPage(@PathVariable("page") int page,@PathVariable("size") int size){
        Page<CmsBanner> iPage=new Page<>(page,size);
        IPage<CmsBanner> cmsBannerIPage = cmsBannerService.page(iPage, null);
        return R.ok().data("list",cmsBannerIPage.getRecords()).data("total",cmsBannerIPage.getTotal());
    }

    //新增
    @PostMapping("addBanner")
    public R addBanner(@RequestBody CmsBanner cmsBanner){
        cmsBannerService.save(cmsBanner);
        return R.ok();
    }

    //根据id获取
    @GetMapping("getById/{id}")
    public R getById(@PathVariable("id") String id){
        CmsBanner cmsBanner = cmsBannerService.getById(id);
        return R.ok().data("banner",cmsBanner);
    }

    //修改
    @PostMapping("updateBanner")
    public R updateBanner(@RequestBody CmsBanner cmsBanner){
        cmsBannerService.updateById(cmsBanner);
        return R.ok();
    }

    //删除
    @DeleteMapping("deleteById/{id}")
    public R deleteById(@PathVariable("id") String id){
        cmsBannerService.removeById(id);
        return R.ok();
    }
}

